package com.doeis.manage.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.doeis.core.SuperAction;
import com.doeis.core.bean.Msg;
import com.doeis.manage.bean.Catalog;
import com.doeis.manage.service.CatalogService;
import com.doeis.tools.DateHelper;
import com.doeis.tools.StringHelper;

/**
 * 
 * @author wxy
 * @date 2016-1-6
 */
@WebServlet(urlPatterns = "/catalog/*")
public class CatalogAction extends SuperAction{

	private static final long serialVersionUID = 1L;
	private CatalogService catalogService = new CatalogService();

	@Override
	protected void doRequest(String methodName, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if ("edit".equals(methodName)) {
			edit(request, response);
		} else if ("catList".equals(methodName)) {
			catList(methodName, request, response);
		} else if ("save".equals(methodName)) {
			save(methodName, request, response);
		} else if ("delete".equals(methodName)) {
			delete(methodName, request, response);
		} else {
			toPage("index", "index", request, response);
		}
	}

	private void delete(String methodName, HttpServletRequest request, HttpServletResponse response) {
		try {
			String cat_id = request.getParameter("cat_id");
			Msg msg = new Msg(false, "删除失败");
			boolean result = catalogService.delete(Integer.parseInt(cat_id));
			if (result) {
				msg.setStatus(true);
				msg.setMsg("删除成功");
			}
			sendJsonObject(msg, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void save(String methodName, HttpServletRequest request, HttpServletResponse response) {
		try {
			String item_id = request.getParameter("item_id");
			String cat_id = request.getParameter("cat_id");
			String cat_name = request.getParameter("cat_name");
			String order = request.getParameter("order");
			Msg msg = new Msg(false, "查询失败");
			Catalog catalog = null;
			if (StringHelper.isNull(cat_id)) {
				catalog = new Catalog();
				catalog.setAddTime(DateHelper.getTimestampNowDate());
				catalog.setCat_name(cat_name);
				catalog.setItem_id(Integer.parseInt(item_id));
				catalog.setSort_num(order != null ? Integer.parseInt(order) : 0);
				catalogService.add(catalog);
				msg.setStatus(true);
				msg.setMsg("插入成功");
			} else {// 修改
				catalog = catalogService.findById(Integer.parseInt(cat_id));
				catalog.setCat_name(cat_name);
				catalog.setSort_num(order != null ? Integer.parseInt(order) : 0);
				catalogService.update(catalog);
				msg.setStatus(true);
				msg.setMsg("修改成功");
			}
			sendJsonObject(msg, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 目录列表
	 * 
	 * @param methodName
	 * @param request
	 * @param response
	 * @author wxy 2016年3月21日星期一中国标准时间上午11:44:58
	 * @throws Exception 
	 * @throws NumberFormatException 
	 */
	private void catList(String methodName, HttpServletRequest request, HttpServletResponse response) throws ServletException {
		try {
			String item_id = request.getParameter("item_id");
			Msg msg = new Msg(false, "查询失败");
			if (StringHelper.isNull(item_id)) {
				msg.setMsg("item_id不能为空");
				sendJsonObject(msg, response);
				return;
			}
			List<Catalog> list = catalogService.list(Integer.parseInt(item_id), null);
			msg.setStatus(true);
			msg.setMsg("查询成功");
			msg.setObj(list);
			sendJsonObject(msg, response);
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}

	/**
	 * 跳转到管理页面
	 * 
	 * @Title: toIndex
	 * @data:2014-1-6上午09:40:37
	 * @author:xieyan
	 *
	 * @param request
	 * @param response
	 */
	private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String cat_id = request.getParameter("cat_id");
			if (StringHelper.isNotNull(cat_id)) {
				Catalog catalog = catalogService.findById(Integer.parseInt(cat_id));
				request.setAttribute("catalog", catalog);
			}
			request.getRequestDispatcher(PAGE_PATH + "/catalog/edit.jsp").forward(request, response);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
